// Estimation of Anticipation Model -- Table 6
clear
clear all	
// Loading Data
use ../dta/model_estimation_data.dta, clear
tab date2, gen(date2_dum)
tset code date2

// Non-Linear Estimation - Excess Price Changes - Base
nl exp2: excess_price_diff t if  estimation_sample==1 & vintage!=0 & vintage!=1  , variables(excess_price_diff t) 
eststo nl_epd
disp "theta"
disp _b[/b2]
disp "Direct Effect"
disp _b[/b1]/(1-1/_b[/b2])	
estadd scalar theta=_b[/b2]
estadd scalar de   = _b[/b1]/(1-1/_b[/b2])	

// Non-Linear Estimation - Excess Price Changes - Full
nl exp2: excess_price_diff t if  estimation_sample==1  , variables(excess_price_diff t) 
eststo nl_epd_v2
disp "theta"
disp _b[/b2]
disp "Direct Effect"
disp _b[/b1]/(1-1/_b[/b2])	
estadd scalar theta=_b[/b2]
estadd scalar de   = _b[/b1]/(1-1/_b[/b2])	

// Non-Linear Estimation -TWFE - Base
nl exp2: price_diff_r_tw t if  estimation_sample==1 & vintage!=0 & vintage!=1 , variables(price_diff_r_tw t) 
eststo nl_twfe
disp "theta"
disp _b[/b2]
disp "Direct Effect"
disp _b[/b1]/(1-1/_b[/b2])	
estadd scalar theta=_b[/b2]
estadd scalar de   = _b[/b1]/(1-1/_b[/b2])	

// Non-Linear Estimation -TWFE - Full
nl exp2: price_diff_r_tw t if  estimation_sample==1  , variables(price_diff_r_tw t) 
eststo nl_twfe_v2
disp "theta"
disp _b[/b2]
disp "Direct Effect"
disp _b[/b1]/(1-1/_b[/b2])	
estadd scalar theta=_b[/b2]
estadd scalar de   = _b[/b1]/(1-1/_b[/b2])	


// Linear Estimation - OLS - Base
reghdfe  price590 F.price590 d   if vintage!=1 & vintage!=0 ,cl(code) a(code date2) nocons
eststo ab_1
estadd scalar theta=1/_b[F.price590]	
estadd scalar de   = _b[d]/(1-_b[F.price590])	
disp "theta"
disp 1/_b[F.price590]	
disp "Direct Effect"
disp _b[d]/(1-_b[F.price590])	


// Linear Estimation - OLS - Full
reghdfe  price590 F.price590 d  ,cl(code) a(code date2) nocons
eststo ab_1_all
estadd scalar theta=1/_b[F.price590]	
estadd scalar de   = _b[d]/(1-_b[F.price590])			 
disp "theta"
disp 1/_b[F.price590]	
disp "Direct Effect"
disp _b[d]/(1-_b[F.price590])		 
 
// 2SLS - OLS - Base
ivreghdfe price590 (F.price590=f(2/3).d) d if vintage!=1 & vintage!=0 ,cl(code ) a(code date2)
eststo ab_2
 estadd scalar theta=1/_b[F.price590]	
 estadd scalar de   = _b[d]/(1-_b[F.price590])	
 estadd scalar fstat=e(widstat) 
disp "theta"
disp 1/_b[F.price590]	
disp "Direct Effect"
disp _b[d]/(1-_b[F.price590])	
 
// 2SLS - OLS - Full 
ivreghdfe price590 (F.price590=f(2/3).d) d  ,cl(code) a(code date2)
eststo ab_2_all
 estadd scalar theta=1/_b[F.price590]	
 estadd scalar de   = _b[d]/(1-_b[F.price590])	
 estadd scalar fstat=e(widstat) 
disp "theta"
disp 1/_b[F.price590]	
disp "Direct Effect"
disp _b[d]/(1-_b[F.price590])	


// Outputting results
esttab 		 nl_epd nl_epd_v2 nl_twfe  nl_twfe_v2  ab_1 ab_1_all ab_2 ab_2_all  /// ab_4
			using ../esttab/tab6.tex, replace  ///
			stats(theta de fstat , fmt(a3 a3 a1) labels("$ \theta $ "  "Direct Effect" "First Stage F-Stat (Kleibergen-Paap)") ///
			layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}") )	 	/// 	
			b(3) se(3)  booktabs compress  alignment(D{.}{.}{-1}) keep(_cons F.price590) eqlabels(none)  rename( d _cons ) ///
			 varlabels(b2:_cons "$ {\theta} $" _cons "$ \delta_1 $ " F.price590   "$ \frac{1}{\theta} $")   order(b2:_cons F.price590    _cons) ///
			wrap  varwidth(30) frag  nomtitles nonumbers  noobs nonotes  substitute(_ _) star(* 0.10 ** 0.05 *** .01)  	

